package com.rlz.base.util;

import android.view.View;

import com.rlz.base.tools.adapter.BaseAdapter;
import com.rlz.base.tools.refresh.RefreshLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by RLZ
 * on 2021/4/8
 * 数据帮助类 快速设置数据
 */
public class DataHelper {
    private DataHelper() {
    }


    /**
     * 设置不带分页的数据
     */
    public static <T> void initDataNoPager(List<?> arrayList, List<T> dataList, BaseAdapter adapter) {
        if (dataList != null && dataList.size() > 0) ((List<T>) arrayList).addAll(dataList);
        adapter.notifyDataSetChanged();
    }


    public static <T> void initDataNoPager(List<?> arrayList, List<T> dataList, BaseAdapter adapter, View emptyView) {
        initDataNoPager(arrayList, dataList, adapter);
        if (emptyView != null) isShowEmpty((ArrayList<?>) arrayList, emptyView);
    }


    public static <T> void setData(int curPage, List<?> arrayList, List<T> dataList, BaseAdapter adapter, RefreshLayout refreshLayout, View emptyView) {
        setData(curPage, arrayList, dataList, adapter, refreshLayout);
        if (emptyView != null) isShowEmpty((ArrayList<?>) arrayList, emptyView);
    }

    /**
     * 设置带 分页的数据
     */
    public static <T> void setData(int curPage, List<?> arrayList, List<T> dataList,
                                   BaseAdapter adapter, RefreshLayout refreshLayout) {
        if (curPage == 0) arrayList.clear();

        if (dataList != null && dataList.size() > 0) ((List<T>) arrayList).addAll(dataList);
        else if (refreshLayout != null)
            refreshLayout.finishRefreshing();

        if (curPage == 0)
            adapter.notifyDataSetChanged();
        else if (arrayList.size() == 0)
            adapter.notifyDataSetChanged();
        else if (dataList != null && dataList.size() > 0)
            adapter.notifyItemRangeChanged(arrayList.size() - dataList.size(), dataList.size());

        if (refreshLayout != null) {
            refreshLayout.finishRefreshing();
            refreshLayout.finishLoadmore();
        }
    }


    public static void notifyItemRemoved(int removePosition, BaseAdapter adapter, ArrayList<?> arrayList, View emptyView) {
        notifyItemRemoved(removePosition, adapter, arrayList);
        if (emptyView != null) isShowEmpty(arrayList, emptyView);
    }


    public static void notifyItemRemoved(int removePosition, BaseAdapter adapter, ArrayList<?> arrayList) {
        adapter.notifyItemRemoved(removePosition);
        adapter.notifyItemRangeChanged(removePosition, adapter.getItemCount());
        arrayList.remove(removePosition);
    }


    public static void isShowEmpty(ArrayList<?> arrayList, View view) {
        if (arrayList != null && arrayList.size() > 0)
            view.setVisibility(View.GONE);
        else view.setVisibility(View.VISIBLE);
    }
}
